<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BOnlineAppointment
 *
 * @author I062261
 */
class BTourBox extends BApp {
    protected $eventTypeList = array();
    protected $messageListViewByEventType = array();
    public function getEventList($eventContext = NULL, $data = NULL) {
        //if no argument, return first level of eventTypes
        $list = array();
        if ($eventContext == NULL) {
            $list = BWEvent::findAll();
        }

        return $list;
    }
    public function run($data = NULL) {
        parent::run($data);

        $eventType = new BWEventGroup();
        $count = $eventType->getRepository()->getCount();

        //init event types for app
        if ($count < 2) {
            $root = BWEventGroup::getRoot();
            $root->addChild('type.travel.translate');
            $root->addChild('type.travel.note');
            $root->addChild('type.travel.food');
            $root->addChild('type.travel.info');
            $root->addChild('type.travel.ad');
        }

        //init messageList view 
        $eventTypeList = $this->getEventTypeList('fullName');

        foreach ($eventTypeList as $id => $name) {
            if ($name == 'travel.translate') {
                $this->messageListViewByEventType[$id] = 'travelTranslate_messageList';
            }
        }
    }
    public function getEventTypeList($data = NULL) {
        if ($this->eventTypeList == NULL)
            $this->eventTypeList = BWEventGroup::getRoot()->getChild('type')->getChildrenAsLeaf();

        if (is_string($data) && strtolower($data) == 'fullname') {
            $list = array();
            foreach ($this->eventTypeList as $id => $type) {
                $name = $type->getFullName('|');
                //remove 'type|' in full name
                $name = substr($name, 5);
                $list[$id] = $name;
            }
            return $list;
        }
        return $this->eventTypeList;
    }
    public function getMessageListViewByEventType($eventType) {

        $eventTypeId = NULL;

        if ($eventType instanceof BWEventGroup)
            $eventTypeId = $eventType->getId();

        if (is_string($eventType)) {
            $eventType = BWEventGroup::find($eventType);
            if ($eventType instanceof BWEventGroup)
                $eventTypeId = $eventType->getId();
        }

        if (is_numeric($eventType))
            $eventTypeId = $eventType;

        if (isset($this->messageListViewByEventType[$eventTypeId]))
            return $this->messageListViewByEventType[$eventTypeId];

        return FALSE;
    }
    public function findEventList($findContext = NULL, $onlyPermitted = TRUE) {

        if ($findContext == NULL) {
            $eventList = BWEvent::findAll();
            return $eventList;
        }

        $eventList = arra();

        if (is_array($findContext)) {
            //find by type
            if (isset($findContext['type'])) {
                $type = $findContext['type'];
                unset($findContext['type']);
                if (is_string($type)) {
                    $type = BWEventGroup::find($type);
                }
                if ($type instanceof BWEventGroup) {
                    $eventList = $type->getMembers($findContext);
                    //return $eventList;
                }
            }

            if (isset($findContext['vender'])) {
                
            }

            if (isset($findContext['bookable'])) {
                
            }

            if (isset($findContext['booking'])) {
                
            }

            if (isset($findContext['description'])) {
                
            }
        }
    }
    public function runManageTourBox($operation = NULL, $data = NULL) {        

        if (yii::app()->request->isAjaxRequest) {
            $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
            $senderControl = Blyn::app()->getUIManager()->getSenderControl();
             $processData = new BWProcessData();

            if ($senderControl->isAjaxLinkClicked('Submit')) {
                $model = new BTBManageTourBoxFormModel();
                if (!empty($_POST)) {
                    $model->attributes = $_POST['BTBManageTourBoxFormModel'];
                    $nativeSentence = $model->nativeSentence;
                    $foreignSentence = $model->foreignSentence;
                    $nativeLanguage = NULL;
                    $foreignLanguage = NULL;

                    $translation = new BTTranslation();
                    if ($model->nativeLanguage != NULL) {
                        $nativeLanguageId = $model->nativeLanguage;
                        $obj = new BTLanguage($nativeLanguageId);
                        $nativeLanguage = $obj->name;
                        $translation->nativeLanguage = $nativeLanguage;
                    }

                    if ($model->foreignLanguage != NULL) {
                        $foreignLanguageId = $model->foreignLanguage;
                        $obj = new BTLanguage($foreignLanguageId);
                        $foreignLanguage = $obj->name;
                        $translation->foreignLanguage = $foreignLanguage;
                    }

                    $translation->nativeSentence = $nativeSentence;
                    $translation->foreignSentence = $foreignSentence;
                    $translation->save();

                    //add category
                    if ($model->nativeCategory != NULL) {
                        $categoryId = $model->nativeCategory;
                        $category = new BTCategory($categoryId);
                        //$category->load(array());

                        if ($category instanceof BTCategory) {
                            $nativeSentence = $translation->nativeSentence;
                            if ($nativeSentence instanceof BTSentence) {
                                $category->addMember($nativeSentence);
                            }
                        }
                    }

                    Blyn::app()->getUIManager()->getCurrentScreen()->addRenderView('manageTourBox', 'manageTourBox', array(
                        'nativeCategoryList' => $this->getCategoryListData($nativeLanguage),
                        'languageList' => $this->getLanguageListData(),
                        'nativeLanguage' => $nativeLanguage,
                        'addResult' => array(
                            'result' => 'Add translation successfully!',
                            'nativeSentence' => $nativeSentence,
                            'foreignSentence' => $foreignSentence
                        )
                    ));
                }
            }
            if ($senderControl->isAjaxLinkClicked('showAddEvent')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $updateControl->addRenderView('addEventView', 'event_add', array('eventId' => $eventId));
            }
            if ($senderControl->isAjaxLinkClicked('saveEvent')) {
                $modelName = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('postModel');
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $postData = $processData->parsedModelData($modelName);
                $event = $processData->populateEventFromPostData($postData);
                $event->save();
                $eventType = $processData->populateEventTypeFromPostData($postData);
                if ($eventType instanceof BWEventGroup) {
                    $eventType->addMember($event);
                }

                $mediaList = $processData->populateMediaListFromPostData($postData, 'event');

                //save media to event
                foreach ($mediaList as $media) {
                    $event->getMediaManager()->addMedia($media);
                }
                if (!$updateControl instanceof BUIScreen) {
                    $updateControl->addRenderView('eventList', 'event_list', array('eventList' => $this->getEventList()));
                }
                $updateControl->render();
        }
            if ($senderControl->isAjaxLinkClicked('showMessageList')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $messageListView = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageListView');
                if( $messageListView == 'showTranslationList'){
                    $language = new BTLanguage();
                    $nativeLanguage = $language->getNativeLanguage();
                    $nativeCategoryList = $this->getCategoryListData($nativeLanguage);
                    $languageList = $this->getLanguageListData();
                    $updateControl->addRenderView('messageList', $messageListView, array(
                        'nativeCategoryList' => $nativeCategoryList,
                        'languageList' => $languageList,
                        'nativeLanguage' => $nativeLanguage
                    ));
                }  else {
                    $updateControl->addRenderView('messageList', $messageListView, array('eventId' => $eventId));
            
                    }
                }
            if ($senderControl->isAjaxLinkClicked('showEventList')) {
            $eventLit = $this->getEventList();
            $updateControl->addRenderView('eventList', 'event_list', array('eventList' => $eventLit));
            //$updateControl->render();
            //Blyn::app()->end();
        }
            if ($senderControl->isAjaxLinkClicked('showAddNote')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
                $updateControl->addRenderView('note_add', 'note_add', array(
                    'eventId' => $eventId,
                    'messageId' => $messageId
                ));
                //$updateControl->render();
                //Blyn::app()->end();
            }
            if ($senderControl->isAjaxLinkClicked('showAddFood')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
                $updateControl->addRenderView('food_add', 'food_add', array(
                    'eventId' => $eventId,
                    'messageId' => $messageId
                ));
                //$updateControl->render();
                //Blyn::app()->end();
            }
            if ($senderControl->isAjaxLinkClicked('showAddInfo')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
                $updateControl->addRenderView('info_add', 'info_add', array(
                    'eventId' => $eventId,
                    'messageId' => $messageId
                ));
                //$updateControl->render();
                //Blyn::app()->end();
            }
            if ($senderControl->isAjaxLinkClicked('showAddAd')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
                $updateControl->addRenderView('ad_add', 'ad_add', array(
                    'eventId' => $eventId,
                    'messageId' => $messageId
                ));
                //$updateControl->render();
                //Blyn::app()->end();
            }
            if ($senderControl->isAjaxLinkClicked('showNoteList')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $updateControl->addRenderView('showTravelNotesList', 'showTravelNotesList', array('eventId' => $eventId));
                //Blyn::app()->getUIManager()->getCurrentScreen()->addControl($updateControl);
                //$updateControl->render();
                //Blyn::app()->end();
            }
            if ($senderControl->isAjaxLinkClicked('showInfoList')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $updateControl->addRenderView('showTravelInfoList', 'showTravelInfoList', array('eventId' => $eventId));
                //Blyn::app()->getUIManager()->getCurrentScreen()->addControl($updateControl);
                //$updateControl->render();
                //Blyn::app()->end();
            }
            if ($senderControl->isAjaxLinkClicked('showAdList')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $updateControl->addRenderView('showTravelAdList', 'showTravelAdList', array('eventId' => $eventId));
                //Blyn::app()->getUIManager()->getCurrentScreen()->addControl($updateControl);
                //$updateControl->render();
                //Blyn::app()->end();
            }
            if ($senderControl->isAjaxLinkClicked('showFoodList')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $updateControl->addRenderView('showTravelFoodList', 'showTravelFoodList', array('eventId' => $eventId));
                //Blyn::app()->getUIManager()->getCurrentScreen()->addControl($updateControl);
                //$updateControl->render();
                //Blyn::app()->end();
            }
            if ($senderControl->isAjaxLinkClicked('saveMessage')) {
                $modelName = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('postModel');
                $postData = $processData->parsedModelData($modelName);
                $parentEventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('parentEventId');
                $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
                $parent = NULL;
                $message = $this->populateMessageFromPostData($postData, $messageId);
                $message->save();
                if ($parentEventId != NULL && $parentEventId > 0) {
                    $parent = new BWEvent($parentEventId);
                }

                if ($parent != NULL) {
                    //$event = new BWEvent($eventId);
                    $parent->getMessageManager()->addMessage($message); //添加关连
                }

               $mediaList = $processData->populateMediaListFromPostData($postData);
                //save media to message
                foreach ($mediaList as $media) {
                    $message->getMediaManager()->addMedia($media);
                }
                if ($parent != NULL) {
                    $eventTypeName = $parent->getGroup(array('parent' => BWEventGroup::EVENT_TYPE))->name;
                    switch($eventTypeName)
                    {
                        case 'translate': $messageListView = 'showTranslationList'; break;
                        case 'note': $messageListView = 'showTravelNotesList'; break;
                        case 'food': $messageListView = 'showTravelFoodList'; break;
                        case 'info': $messageListView = 'showTravelInfoList'; break;
                        case 'ad': $messageListView = 'showTravelAdList'; break;
                        default : $messageListView = 'showTranslationList';
                    }
                    $updateControl->addRenderView($messageListView, $messageListView, array('eventId' => $parentEventId));
                }
            }
//            else {//default action
//                $language = new BTLanguage();
//                $nativeLanguage = $language->getNativeLanguage();
//                $nativeCategoryList = $this->getCategoryListData($nativeLanguage);
//                $languageList = $this->getLanguageListData();
//                Blyn::app()->getUIManager()->getCurrentScreen()->addRenderView('manageTourBox', 'manageTourBox', array(
//                    'nativeCategoryList' => $nativeCategoryList,
//                    'languageList' => $languageList,
//                    'nativeLanguage' => $nativeLanguage
//                ));
//                Blyn::app()->getUIManager()->getCurrentScreen()->addRenderView('manageTourBox', 'manageTourBox');
//            }
        } else {
            $senderControl = Blyn::app()->getUIManager()->getSenderControl();
            //$screen = Blyn::app()->getUIManager()->getCurrentScreen();
            if ($senderControl->isLinkClicked('Test Wechat Page')) {
                $translation = new BTTranslation();
                $sql = "select t._id as id,e.sentence as en_US,c.sentence as zh_CN,ca.name as category from tbl_translation t, tbl_sentence_english e,tbl_sentence_chinese c,tbl_category ca, tbl_category_member m where t._id = e.translation_id and t._id = c.translation_id and m.member_id = c._id and m.category_id = ca._id and m.member_type = 'zh_CN'";
                $viewOfChineseCategory = $translation->getRepository()->getView(array('sql' => $sql, 'params' => array(":MemberType" => 'MBTSentenceChinese')));
                $data = array();
                foreach ($viewOfChineseCategory->rows as $row) {
                    array_push($data, $row->attributes);
                }
                $viewJSon = CJSON::encode($data);
                echo $viewJSon;
                yii::app()->end();
                //$screen->setViews(NULL);
                //$screen->addRenderView('followTourBox', 'followTourBox',array('viewJSon'=>$viewJSon));
            }
        }
    }

    public function runFollowTourBox($operation = NULL, $data = NULL) {
        $processData = new BWProcessData();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $senderControl = Blyn::app()->getUIManager()->getSenderControl();
        $currentScreen = Blyn::app()->getUIManager()->getCurrentScreen();

        $wxMenu = new BUIWXMenu(Blyn::app()->getActiveOperation());
        $followBookingPage = new BUIWXControl();
        $currentScreen->addChildControl($followBookingPage);

        if ($wxMenu->isMenuClicked('旅游攻略')) {
            $App =  Blyn::app()->getActiveApp();
            $InfoList = $App->getEventList();
            if(empty($InfoList)){//如果$InfoList为空(意味着没有数据)，则提示信息。
                   echo '<script language="javascript">';
                   echo 'alert("无数据，请于manageTourBox添加数据！");';
                   echo '</script>';
            }
            foreach ($InfoList as $id => $event){
                $eventTypeName = $event->getGroup(array('parent' => BWEventGroup::EVENT_TYPE))->name;
                if($eventTypeName=='note')  {
                       $event = new BWEvent($id);
                       $messageList = $event->getMessageManager()->getMessageList();
                       $followBookingPage->addRenderView('travel_note_list', 'travel_note_list',array('messageList'=>$messageList));
                 }
            }
        }
        if ($wxMenu->isMenuClicked('旅游美食')) {
            $App =  Blyn::app()->getActiveApp();
            $InfoList = $App->getEventList();
            if(empty($InfoList)){//如果$InfoList为空(意味着没有数据)，则提示信息。
                   echo '<script language="javascript">';
                   echo 'alert("无数据，请于manageTourBox添加数据！");';
                   echo '</script>';
            }
            foreach ($InfoList as $id => $event){
                $eventTypeName = $event->getGroup(array('parent' => BWEventGroup::EVENT_TYPE))->name;
                if($eventTypeName=='food')  {
                       $event = new BWEvent($id);
                       $messageList = $event->getMessageManager()->getMessageList();
                       $followBookingPage->addRenderView('travel_food_list', 'travel_food_list',array('messageList'=>$messageList));
                 }
            }
        }
        if ($wxMenu->isMenuClicked('旅游信息')) {
            $App =  Blyn::app()->getActiveApp();
            $InfoList = $App->getEventList();
            if(empty($InfoList)){//如果$InfoList为空(意味着没有数据)，则提示信息。
                   echo '<script language="javascript">';
                   echo 'alert("无数据，请于manageTourBox添加数据！");';
                   echo '</script>';
            }
            foreach ($InfoList as $id => $event){
                $eventTypeName = $event->getGroup(array('parent' => BWEventGroup::EVENT_TYPE))->name;
                if($eventTypeName=='info')  {
                       $event = new BWEvent($id);
                       $messageList = $event->getMessageManager()->getMessageList();
                       $followBookingPage->addRenderView('travel_info_list', 'travel_info_list',array('messageList'=>$messageList));
                 }
            }
        }
        if ($wxMenu->isMenuClicked('旅游广告')) {
            $App =  Blyn::app()->getActiveApp();
            $InfoList = $App->getEventList();
            if(empty($InfoList)){//如果$InfoList为空(意味着没有数据)，则提示信息。
                   echo '<script language="javascript">';
                   echo 'alert("无数据，请于manageTourBox添加数据！");';
                   echo '</script>';
            }
            foreach ($InfoList as $id => $event){
                $eventTypeName = $event->getGroup(array('parent' => BWEventGroup::EVENT_TYPE))->name;
                if($eventTypeName=='ad')  {
                       $event = new BWEvent($id);
                       $messageList = $event->getMessageManager()->getMessageList();
                       $followBookingPage->addRenderView('travel_ad_list', 'travel_ad_list',array('messageList'=>$messageList));
                 }
            }
        }
        if($wxMenu->isMenuClicked('旅游管理')){
            $followBookingPage->addRenderView('manageTravel','manageTravel');
        }
        if ($senderControl->isLinkClicked('tourBoxNoteDetail')) {
            $messageID = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageID');
            $messageName = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageName');
            $messageDsc = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageDsc');
            $followBookingPage->addRenderView('travel_note_detail', 'travel_note_detail', array('messageID' => $messageID, 'messageName' => $messageName, 'messageDsc' => $messageDsc));
        }
        if ($senderControl->isLinkClicked('showjqAddEvent')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $followBookingPage->addRenderView('jq_event_add', 'jq_event_add', array('eventId' => $eventId));
          }
        if ($senderControl->isLinkClicked('savejqEvent')) {
            $modelName = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('postModel');
            $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
            $postData = $processData->parsedModelData($modelName);
            $event = $processData->populateEventFromPostData($postData);
            $event->save();
            $eventType = $processData->populateEventTypeFromPostData($postData);
            if ($eventType instanceof BWEventGroup) {
                $eventType->addMember($event);
            }

            $mediaList = $processData->populateMediaListFromPostData($postData, 'event');

            //save media to event
            foreach ($mediaList as $media) {
                $event->getMediaManager()->addMedia($media);
            }
            if (!$updateControl instanceof BUIScreen) {
                $followBookingPage->addRenderView('eventList', 'event_list', array('eventList' => $this->getEventList()));
            }
            $followBookingPage->render();
        }
        if ($senderControl->isLinkClicked('showjqMessageList')) {
                $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
                $messageListView = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageListView');
                if( $messageListView == 'showTranslationList'){
                    $language = new BTLanguage();
                    $nativeLanguage = $language->getNativeLanguage();
                    $nativeCategoryList = $this->getCategoryListData($nativeLanguage);
                    $languageList = $this->getLanguageListData();
                    $followBookingPage->addRenderView('messageList', $messageListView, array(
                        'nativeCategoryList' => $nativeCategoryList,
                        'languageList' => $languageList,
                        'nativeLanguage' => $nativeLanguage
                    ));
                }  else {
                    $followBookingPage->addRenderView('messageList', $messageListView, array('eventId' => $eventId));
            
                    }
                }
        if ($senderControl->isLinkClicked('showjqAddNote')) {
            $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
            $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
            $followBookingPage->addRenderView('jq_note_add', 'jq_note_add', array(
                'eventId' => $eventId,
                'messageId' => $messageId
            ));
            //$updateControl->render();
            //Blyn::app()->end();
        }
        if ($senderControl->isLinkClicked('showjqAddFood')) {
            $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
            $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
            $followBookingPage->addRenderView('jq_food_add', 'jq_food_add', array(
                'eventId' => $eventId,
                'messageId' => $messageId
            ));
            //$updateControl->render();
            //Blyn::app()->end();
        }
        if ($senderControl->isLinkClicked('showjqAddInfo')) {
            $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
            $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
            $followBookingPage->addRenderView('jq_info_add', 'jq_info_add', array(
                'eventId' => $eventId,
                'messageId' => $messageId
            ));
            //$updateControl->render();
            //Blyn::app()->end();
        }
        if ($senderControl->isLinkClicked('showjqAddAd')) {
            $eventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('eventId');
            $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
            $followBookingPage->addRenderView('jq_ad_add', 'jq_ad_add', array(
                'eventId' => $eventId,
                'messageId' => $messageId
            ));
            //$updateControl->render();
            //Blyn::app()->end();
        }
        if ($senderControl->isLinkClicked('savejqMessage')) {
                $modelName = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('postModel');
                $postData = $processData->parsedModelData($modelName);
                $parentEventId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('parentEventId');
                $messageId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageId');
                $parent = NULL;
                $message = $this->populateMessageFromPostData($postData, $messageId);
                $message->save();
                if ($parentEventId != NULL && $parentEventId > 0) {
                    $parent = new BWEvent($parentEventId);
                }

                if ($parent != NULL) {
                    //$event = new BWEvent($eventId);
                    $parent->getMessageManager()->addMessage($message); //添加关连
                }

               $mediaList = $processData->populateMediaListFromPostData($postData);
                //save media to message
                foreach ($mediaList as $media) {
                    $message->getMediaManager()->addMedia($media);
                }
                if ($parent != NULL) {
                    $eventTypeName = $parent->getGroup(array('parent' => BWEventGroup::EVENT_TYPE))->name;
                    switch($eventTypeName)
                    {
                        case 'translate': $messageListView = 'showTranslationList'; break;
                        case 'note': $messageListView = 'showTravelNotesList'; break;
                        case 'food': $messageListView = 'showTravelFoodList'; break;
                        case 'info': $messageListView = 'showTravelInfoList'; break;
                        case 'ad': $messageListView = 'showTravelAdList'; break;
                        default : $messageListView = 'showTranslationList';
                    }
                    $followBookingPage->addRenderView($messageListView, $messageListView, array('eventId' => $parentEventId));
                }
            }
        if ($senderControl->isLinkClicked('tourBoxFoodDetail')) {
            $messageID = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageID');
            $messageName = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageName');
            $messageDsc = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageDsc');
            $followBookingPage->addRenderView('travel_food_detail', 'travel_food_detail', array('messageID' => $messageID, 'messageName' => $messageName, 'messageDsc' => $messageDsc));
        }
        if ($senderControl->isLinkClicked('tourBoxInfoDetail')) {
            $messageID = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageID');
            $messageName = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageName');
            $messageDsc = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageDsc');
            $followBookingPage->addRenderView('travel_info_detail', 'travel_info_detail', array('messageID' => $messageID, 'messageName' => $messageName, 'messageDsc' => $messageDsc));
        }
        if ($senderControl->isLinkClicked('tourBoxAdDetail')) {
            $messageID = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageID');
            $messageName = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('messageName');
            $followBookingPage->addRenderView('travel_ad_detail', 'travel_ad_detail', array('messageID' => $messageID, 'messageName' => $messageName));
        }
    }

    public function runManageTravelNotes($operation = NULL, $data = NULL) {
        $eventList = BTEvent::findAll(array('type' => 'travelNotes'));
        Blyn::app()->getUIManager()->getCurrentScreen()->addRenderView('travelNotesList', 'travelNotesList', array('eventList' => $eventList));
    }

    public function getCategoryListData($scope = 'zh_CN', $parent = null) {
        $language = new BTLanguage($scope);
        if ($language->getId() == NULL) {
            $language->save();
        }

        $category = $language->getCategory();
        $children = $category->getChildrenAsLeaf();
        if (empty($children)) {
            $category->addChild('food');
            $category->addChild('landScape');
            $children = $category->getChildrenAsLeaf();
        }

        $list = array();

        foreach ($children as $id => $child) {
            $list[$id] = $child->getName();
        }

        return $list;
    }

    public function getLanguageListData($data = NULL) {
        $languageList = BTLanguage::findAll();
        $list = array();
        if (empty($languageList) || count($languageList) < 2) {
            $lang = new BTLanguage();
            $lang->name = BTLanguage::CHINESE;
            $lang->save();
            $languageList[$lang->getId()] = $lang;
            $lang = new BTLanguage();
            $lang->name = BTLanguage::ENGLIST;
            $lang->save();
            $languageList[$lang->getId()] = $lang;
        }


        foreach ($languageList as $id => $language) {
            $list[$id] = $language->getName();
        }
        return $list;
    }

    public function getDbConnection($data = NULL) {
        return yii::app()->tourbox;
    }
    protected function populateMessageFromPostData($postData, $messageIndex = NULL, $message = NULL) {
        if (!$message instanceof BWMessage)
            $message = new BWMessage ();

        foreach ($postData as $data) {
            $type = $data['type'];
            $class = $data['class'];
            $name = $data['name'];
            $value = $data['value'];
            $index = isset($data['index']) ? $data['index'] : NULL;
            if ($class == 'message') {
                if ($messageIndex != NULL && $index != NULL) {
                    if (property_exists('BWMessage', $name) && $type == 'string' && $index == $messageIndex) {
                        $message->$name = $value;
                    }
                } else {
                    //if not set eventId
                    if (property_exists('BWMessage', $name) && $type == 'string') {
                        $message->$name = $value;
                    }
                }
            }
        }
        return $message;
    }
}

?>
